<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>教师审核</title>
<link rel="stylesheet" href="${BasePath}/css/bootstrap.min.css">
<link rel="stylesheet" href="${BasePath}/css/bootstrap-pager.css">
<link rel="stylesheet" href="${BasePath}/css/common.css?v=${cssVersion}">
<link type="text/css" href="${BasePath}/admin/css/theme.css" rel="stylesheet">
<style>
.table tbody tr td{
    vertical-align: middle;
}
</style>
</head>
<body>
    <#include '/admin/navAdmin.html'>
	<div class="panel panel-default">
		<div class="panel-heading">
	        <h3 class="panel-title"> <strong>教师列表（管理员端）</strong></h3>
	    </div>
	    <div class="panel-body">
		    <div id="container">
			    <table class="table table-bordered table-hover table-condensed">
					<thead>
						<tr>
							<th>昵称</th>
							<th>头衔</th>
							<th>电话</th>
							<th>地区</th>
							<th>证件</th>
							<th>审核状态</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>
						<#list teachers as t>
						<tr>
							<td>${t.nickName!''}</td>
							<td>${t.individualitySign!''}</td>
							<td>${t.mobile!''}</td>
							<td>${t.city!''}</td>
							<td>
								<#if (t.certPic)??>
									<a href="${(t.certPic)!''}" target="_blank">证件照片</a>
								<#else>
									无
								</#if>
							</td>
							<#if t.auditStatus == 1>
								<td id="auditStatus${t.id}">审核通过</td>
							<#elseif t.auditStatus == 2>
								<td id="auditStatus${t.id}">审核不通过</td>
							<#else>
								<td id="auditStatus${t.id}">未审核</td>
							</#if>
							<td>
								<button class="btn btn-sm btn-warning infoPass" tid="${t.id}">审核通过</button>
								<button class="btn btn-sm btn-warning infoNoPass" tid="${t.id}">审核不通过</button>
								<button class="btn btn-sm btn-info courseAudit" tid="${t.id}">课程审核</button>
								<button class="btn btn-sm btn-info teacherQuestionsAdmin" tid="${t.id}">教师问答</button>
							</td>
						</tr>
						</#list>
					</tbody>
				</table>
		    </div>
		    <div id="page-container-static-big" style="text-align: center;">
				<ul id="pageLimit"></ul>
			</div>
	    </div>
	</div>
	<script type="text/javascript" src="${BasePath}/js/jquery-3.3.1.min.js"></script>  
	<script type="text/javascript" src="${BasePath}/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="${BasePath}/js/bootstrap-paginator.min.js"></script>
	<script type="text/javascript" src="${BasePath}/js/bootstrap-pager.js"></script>
	<script type="text/javascript" src="${BasePath}/js/common-func.js?v=${jsVersion}"></script>
	<script type="text/javascript" src="${BasePath}/js/ajaxHeader.js?v=${jsVersion}"></script>  
	<script>
	
	//禁用返回
    window.history.pushState(null, null, "#");
	window.addEventListener("popstate", function(e) {
		window.history.pushState(null, null, "#");
	});
	
	var pageSize = 10;
	$('#pageLimit').bootstrapPaginator({
		currentPage: 1,
		totalPages: ${totalPages},
		size:"big",
		bootstrapMajorVersion: 3,
		alignment:"right",
		numberOfPages: pageSize,
		itemTexts: function (type, page, current) {
			switch (type) {
			case "first": return "首页";
			case "prev": return "上一页";
			case "next": return "下一页";
			case "last": return "末页";
			case "page": return page;
			}//默认显示的是第一页。
		},
		//给每个页眉绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
		onPageClicked: function (event, originalEvent, type, page){
			var teacherRequest = {pageNum: page, pageSize: pageSize };
			var req = {requestData: JSON.stringify(teacherRequest)};
			$.ajax({
				url: "${BasePath}/api/teacher/teacherList",
				type:'POST',
				contentType : "application/json; charset=utf-8",
				data: JSON.stringify(req),
				dataType:'json',
				success:function (data) {
					if(data == null){
						console.log("没有数据");
						return;
					}
					
					var teachers = JSON.parse(data.responseData);
					
					if(teachers != null){
						var tDoms = '';
						for(var i=0; i<teachers.length; i++){
						    var t = teachers[i];//获取LIST里面的对象
						    tDoms += getTeacherDom(t);
						}
						
						$('tbody').html(tDoms);
						
						regAuditEvents();
					}
				}
			});
		}
	});
	
	$(document).ready(function(){
		regAuditEvents();
	});
	
	function regAuditEvents(){
		$(".infoPass").click(function(){
			var teacherId = $(this).attr("tid");
			
			var dom = $("#auditStatus"+teacherId);
			
			if(dom.html() != '审核通过'){
				var success = teacherAudit(teacherId, true);
				dom.html("审核通过");
			}
			else{
				console.log("操作无效")
			}
		});
		
		$(".infoNoPass").click(function(){
			var teacherId = $(this).attr("tid");
			
			var dom = $("#auditStatus"+teacherId);
			if(dom.html() != '审核不通过'){
				var success = teacherAudit(teacherId, false);
				dom.html("审核不通过");
			}
			else{
				console.log("操作无效")
			}
		});
		
		$(".courseAudit").click(function(){
			var teacherId = $(this).attr("tid");
			window.open("${BasePath}/page/admin/teacher/courses?teacherId=" + teacherId);
		});
		
		$(".teacherQuestionsAdmin").click(function(){
			var teacherId = $(this).attr("tid");
			window.open("${BasePath}/page/admin/teacher/questions?teacherId=" + teacherId);
		});
	}
	
	//教师审核
	function teacherAudit(tid, pass){
		if(tid == null || pass == null){
			console.log("param invalid");
			return false;
		}
		var teacherRequest = {teacherId: tid};
		if(pass == true){
			teacherRequest.auditStatus = 1;
		}
		else{
			teacherRequest.auditStatus = 2;
		}
		var req = {requestData: JSON.stringify(teacherRequest)};
		var ret = false;
		$.ajax({
			url: "${BasePath}/api/teacher/audit",
			type:'POST',
			contentType : "application/json; charset=utf-8",
			data: JSON.stringify(req),
			dataType:'json',
			success:function (data) {
				console.log("data=" + JSON.stringify(data));
				if(data == null){
					console.log("没有数据");
					return;
				}
				ret = Boolean(data.responseData);
			}
		});
		return ret;
	}
	
	function getTeacherDom(t){
		var tDom = '';
		tDom += '<tr>\r\n';
		
		tDom += '<td>' + t.nickName + '</td>\r\n';
		tDom += '<td>' + t.individualitySign + '</td>\r\n';
		tDom += '<td>' + t.mobile + '</td>\r\n';
		tDom += '<td>' + t.city + '</td>\r\n';
		if(typeof t.certPic == "undefined" || t.certPic == null){
			tDom += '<td>无</td>\r\n';
		}
		else{
			tDom += '<td><a href=\"' + t.certPic + '\" target=\"_blank\">证件照片</a></td>\r\n';
		}
		
		tDom += '<td id=\"auditStatus' + t.id + '\">';
		if(t.auditStatus == 1){
			tDom += '审核通过';
		}
		else if(t.auditStatus == 2){
			tDom += '审核不通过';
		}
		else{
			tDom += '未审核';
		}
		tDom += '</td>\r\n';
		
		tDom += '<td>\r\n';
		tDom += '<button class=\"infoPass btn btn-warning\" tid=\"' + t.id + '\">通过</button>\r\n';
		tDom += '<button class=\"infoNoPass btn btn-danger\" tid=\"' + t.id + '\">不通过</button>\r\n';
		tDom += '<button class=\"courseAudit btn btn-link\" tid="' + t.id + '\">课程审核</button>\r\n';
		tDom += '</td>\r\n';
		tDom += '</tr>\r\n';
		return tDom;
	}
	</script>
    
</body>
</html>